package view.shop
{
import flash.display.MovieClip;
import flash.events.Event;

import values.events.ShopItemValueEvent;
import values.shop.ShopItemValue;

import view.component.asset.IconProxy;
import view.component.render.AssetItemRenderProxy;

/**
 * ShopItemRenderProxy
 * @author jliang
 * 
 */
public class ShopItemRenderProxy extends AssetItemRenderProxy
{    
    //==========================================================================
    //  Variables
    //==========================================================================
    public function get coster():ShopItemCosterProxy
    {
        return proxys["coster"];
    }
    
    public function get timeLimiter():ShopItemTimeLimiterProxy
    {
        return proxys["timeLimiter"];
    }
    
    public function get countLimiter():ShopItemCountLimiterProxy
    {
        return proxys["countLimiter"];
    }
    
    public function get buyer():ShopItemBuyerProxy
    {
        return proxys["buyer"];
    }
    //==========================================================================
    //  Overridden methods: AssetItemRenderProxy
    //==========================================================================
    protected var vo:ShopItemValue;
    
    override public function set data(value:*):void
    {
        vo = value;
        
        skin.nameLabel.text = vo.name;
        
        //icon
        icon.load(vo.itemValue.template.getIconPathByKey("icon"));
        
        //cost & discount
        coster.data = vo;
        
        //time limit
        timeLimiter.data = vo;
        
        //count limit
        countLimiter.data = vo;
        
        //check buyable
        buyer.data = vo;
        
        if (vo.hasCountLimit)
        {
            vo.addEventListener(ShopItemValueEvent.TOTAL_REST_NUM_CHANGED, totalRestNumChangedHandler);
        }        
        if (vo.hasDailyLimit)
        {
            vo.addEventListener(ShopItemValueEvent.TODAY_REST_NUM_CHANGED, todayRestNumChangedHandler);
        }
        
        check();
    }
    
    override public function get data():*
    {
        return vo;
    }
    
    override protected function get proxyMapping():Array
    {
        return [ 
            { skin:iconSkin, classRef: IconProxy, id:"icon" },
            { skin:"coster", classRef: ShopItemCosterProxy, id:"coster" },
            { skin:"timeLimiter", classRef: ShopItemTimeLimiterProxy, id:"timeLimiter" },
            { skin:"countLimiter", classRef: ShopItemCountLimiterProxy, id:"countLimiter" },
            { skin:"buyer", classRef: ShopItemBuyerProxy, id:"buyer" }
        ];
    }
    
    override public function parse(target:MovieClip):void
    {
        super.parse(target);
        
        coster.render = this;
        timeLimiter.render = this;
        countLimiter.render = this;
        buyer.render = this;
    }
    
    override public function hide():void
    { 
        coster.reset();        
        timeLimiter.reset();        
        countLimiter.reset();
        
        if (vo)
        {
            vo.removeEventListener(ShopItemValueEvent.TOTAL_REST_NUM_CHANGED, totalRestNumChangedHandler);
            vo.removeEventListener(ShopItemValueEvent.TODAY_REST_NUM_CHANGED, todayRestNumChangedHandler);
            
            vo = null;
        }
        
        super.hide();
    }
    //==========================================================================
    //  Public methods
    //==========================================================================    
    public function check():void
    {
        timeLimiter.check();
        countLimiter.check();
        buyer.check();
    }
    
    public function checkBuyable():void
    {
        buyer.check();
    }
    //==========================================================================
    //  Event handlers
    //==========================================================================
    protected function totalRestNumChangedHandler(event:Event):void
    {
        check();
    }
    
    protected function todayRestNumChangedHandler(event:Event):void
    {
        check();
    }
}
}







